from collections import Counter
from math import inf


# Counter的用法 、 all函数、enumerate
# math.inf
# enumerate()是python的内置函数、适用于python2.x和python3.x
# enumerate在字典上是枚举、列举的意思
# enumerate参数为可遍历/可迭代的对象(如列表、字符串)
# enumerate多用于在for循环中得到计数，利用它可以同时获得索引和值
class Solution:
    def balancedString(self, s: str) -> int:
        cnt = Counter(s)
        n = len(s) / 4
        if all(cnt[x] == n for x in "QWER"):
            return 0
        ans, left = inf, 0
        for right, c in enumerate(s):
            cnt[c] -= 1
            while (all(cnt[x] <= n for x in "QWER")):
                ans = min(ans, right - left + 1)
                cnt[s[left]] += 1
                left += 1
        return ans


if __name__ == "__main__":
    solution = Solution()
    print(solution.balancedString("QWEE"))
